package com.aspire.moss.portal.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.aspire.moss.portal.R;

/**
 * 整个桌面的总布局
 * 
 * @author Administrator
 * 
 */
public class DragLayer extends FrameLayout implements MoveIconController {

	private static final String TAG = "DragLayer";
	
	/**
	 * 当长按时放大图标的倍数
	 */
	private static final float ZOOMCELLVIEWICON = 1.25f ;

	private Context mContext;

	Bitmap wallpaper;

	private ImageView moveIconView;

	private Bitmap moveIconBitmap, longClickBitmap; //移动图标图像 , 长按图标图像

	private int moveIconX, moveIconY , moveIconW, moveIconH;//移动图标当前的X ,Y ,宽 ,高
	
	private FrameLayout.LayoutParams moveIconparams;

	private OnTouchEvent mCustomTouchEvent;

	public DragLayer(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public DragLayer(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.DragLayer, defStyle, 0);
		Drawable drawable = a.getDrawable(R.styleable.DragLayer_wallpaper);
		wallpaper = null == drawable ? null : ((BitmapDrawable) drawable)
				.getBitmap();
	}

	public void initMoveIcon(View longClickView) {
		final int longClickLeft = longClickView.getLeft();
		final int longClickTop = longClickView.getTop();
		longClickBitmap = Utils.getViewBitmap(longClickView);//得到longClickView的缩略图
		moveIconBitmap = Utils.scaleBitmap(longClickBitmap, ZOOMCELLVIEWICON);//把longClickView的缩略图放大下,给moveIconBitmap
		moveIconW = moveIconBitmap.getWidth();
		moveIconH = moveIconBitmap.getHeight();
		//计算moveIconView的位置
		final int moveIconLeft = longClickLeft - (moveIconW - longClickBitmap.getWidth()) / 2 ;
		final int moveIconTop = longClickTop - (moveIconH - longClickBitmap.getHeight()) / 2;
		moveIconView = new ImageView(mContext);
		moveIconView.setImageBitmap(moveIconBitmap);
		moveIconparams = new FrameLayout.LayoutParams(moveIconW, moveIconH);
		moveIconparams.gravity = Gravity.TOP;
		moveIconparams.setMargins(moveIconLeft, moveIconTop, moveIconLeft + moveIconW, moveIconTop + moveIconH);
		moveIconView.setLayoutParams(moveIconparams);
		this.addView(moveIconView, moveIconparams);
		moveIconView.bringToFront();
		moveIconView.requestFocus();
		longClickView.setVisibility(View.GONE);
	}

	public void moveIcon(float rawX, float rawY) {
		moveIconX = (int) (rawX - moveIconView.getWidth() / 2);
		moveIconY = (int) (rawY - moveIconView.getHeight() / 2);
		moveIconparams.setMargins(moveIconX, moveIconY, Utils.getScreenWidth(mContext)
				- moveIconX - moveIconW, Utils.getScreenHeight(mContext)
				- moveIconY - moveIconH);
		moveIconView.setLayoutParams(moveIconparams);
		moveIconView.requestFocus();
	}

	public void resetMoveIcon() {
		this.removeView(moveIconView);
		moveIconX = moveIconY = 0;
	}
	
	public int getMoveIconX() {
		return moveIconX;
	}

	public int getMoveIconY() {
		return moveIconY;
	}

	public int getMoveIconWidth() {
		return moveIconW;
	}
	

	public int getMoveIconHeight() {
		return moveIconH;
	}

	/**
	 * 是否拦截该事件,当用记长按,拖动应用时,应该拦截
	 */
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		if (EventCenter.isLongClick) {
			Log.d(TAG, "onInterceptTouchEvent return true");
			return true;
		}
		return super.onInterceptTouchEvent(ev);
	}

	/**
	 * 是否自己处理请该事件,当用户长按时,自己处理,调用onTouchEvent实例处理
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (EventCenter.isLongClick) {
			return mCustomTouchEvent.onDragLayerTouchEvent(event);
		}
		return super.onTouchEvent(event);
	}

	public OnTouchEvent getmCustomTouchEvent() {
		return mCustomTouchEvent;
	}

	public void setmCustomTouchEvent(OnTouchEvent mCustomTouchEvent) {
		this.mCustomTouchEvent = mCustomTouchEvent;
	}

	/**
	 * 处理屏幕框架的事件
	 * 
	 * @author Administrator
	 * 
	 */
	public interface OnTouchEvent {

		/**
		 * 处理拖动图标事件
		 * 
		 * @param event
		 * @return
		 */
		public boolean onDragLayerTouchEvent(MotionEvent event);

	}

}

interface MoveIconController {

	/**
	 *
	 * 初始化移动图标
	 * @param longClickView
	 */
	public void initMoveIcon(View longClickView);
	/**
	 * 移动图标
	 * @param rawX
	 * @param rawY
	 */
	public void moveIcon(float rawX, float rawY);

	/**
	 * 删除移动图标
	 */
	public void resetMoveIcon();
	
	public int getMoveIconX();
	
	public int getMoveIconY();
	
	public int getMoveIconWidth();
	
	public int getMoveIconHeight();

}
